package com.github.bw.capricorn.client.gson;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import java.io.IOException;
import okhttp3.ResponseBody;
import retrofit2.Converter;

public class LenientGsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {

  private final Gson gson;
  private final TypeAdapter<T> adapter;

  public LenientGsonResponseBodyConverter(Gson gson, TypeAdapter<T> adapter) {
    this.gson = gson;
    this.adapter = adapter;
  }

  @Override
  public T convert(ResponseBody value) throws IOException {
    JsonReader jsonReader = gson.newJsonReader(value.charStream());
    jsonReader.setLenient(true);
    try {
      return adapter.read(jsonReader);
    } finally {
      value.close();
    }
  }
}

